package com.xxk.rocket.star.core.annotation.impl;

import com.xxk.rocket.star.annotation.StargateBody;
import com.xxk.rocket.star.core.annotation.AbsProcessAnnotationProducerAndConsume;
import com.xxk.rocket.star.core.dto.StargateBodyDTO;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class StargateBodyProcessAnnotation
        extends AbsProcessAnnotationProducerAndConsume<StargateBodyDTO> {


  @Override
  protected void processConsume(StargateBodyDTO stargateBodyDTO, Class aClass, Object o,
                                Method method)
          throws Throwable {
    processStargate(stargateBodyDTO, aClass, method);
  }

  @Override
  protected void processProducer(StargateBodyDTO stargateBodyDTO, Class aClass, Object o,
                                 Method method) throws Throwable {
    processStargate(stargateBodyDTO, aClass, method);
  }

  private void processStargate(StargateBodyDTO stargateBodyDTO, Class aClass, Method method)
          throws Throwable {
    if (method == null) {
      return;
    }
    Class[] classes = method.getParameterTypes();
    int parameterSize = classes.length;
    Annotation[][] annotations = method.getParameterAnnotations();
    for (int i = 0; i < parameterSize; ++i) {
      for (Annotation annotations1 : annotations[i]) {
        if (annotations1 instanceof StargateBody) {
          stargateBodyDTO.setBodyIndex(i);
          stargateBodyDTO.setBodyClass(classes[i]);
        }
      }
    }
  }
}
